import { Contact } from '../contact';
import {
  ContactState,
  DirectionType,
  DeviceErrorType,
  ContactEventType
} from '../../common/commlib.enums';
import { IMessageParams } from '../../common/commlib.interfaces';

export class MockContact extends Contact {
  public accept() {
    setTimeout(() => {
      this.state = ContactState.CONNECTED;
      this.device.onNewEvent$.next({
        eventType: ContactEventType.CHANGE,
        payload: this
      });
    }, 300);
  }

  public sendMessage(_msgParams?: IMessageParams): any {
    //
  }

  public getPreviousMessages(_params: any): any {
    return new Promise(resolve => {
      resolve();
    });
  }

  public typing(_parameters?: any): any {
    //
  }

  public getDirection() {
    return DirectionType.OTHERS;
  }

  public decline(): any {
    this.state = ContactState.ENDED;
    this.device.onNewEvent$.next({
      eventType: ContactEventType.CHANGE,
      payload: this
    });
  }

  public hold(): any {
    switch (this.state) {
      case ContactState.CONNECTED:
        this.state = ContactState.HOLD;
        break;
      case ContactState.HOLD:
      case ContactState.HOLDANDMUTE:
      default:
        this.device.onError$.next({
          type: DeviceErrorType.HOLD_FAILED,
          message: 'setHold with incorrect state' + this.state.toString,
          payload: null
        });
        return;
    }

    // update state
    this.device.onNewEvent$.next({
      eventType: ContactEventType.CHANGE,
      payload: this
    });
  }

  public unHold(): any {
    switch (this.state) {
      case ContactState.HOLD:
        this.state = ContactState.CONNECTED;
        break;

      case ContactState.HOLDANDMUTE:
        this.state = ContactState.MUTE;
        break;

      default:
        this.device.onError$.next({
          type: DeviceErrorType.UNHOLD_FAILED,
          message: 'call unHold with incorrect state' + this.state.toString,
          payload: null
        });
        return;
    }

    // update state
    this.device.onNewEvent$.next({
      eventType: ContactEventType.CHANGE,
      payload: this
    });
  }

  public mute(): any {
    //
  }

  public unMute(): any {
    //
  }

  public hangup(): any {
    switch (this.state) {
      case ContactState.HOLD:
      case ContactState.CONNECTED:
      case ContactState.HOLDANDMUTE:
        break;

      default:
        this.device.onError$.next({
          type: DeviceErrorType.HANGUP_FAILED,
          message: 'call hangup with incorrect state' + this.state.toString,
          payload: null
        });
        return;
    }

    // update state asny
    setTimeout(() => {
      this.device.onNewEvent$.next({
        eventType: ContactEventType.CHANGE,
        payload: this
      });
    }, 300);
  }
}
